package cn.tede.javagame.shoot;

import cn.tede.javagame.shoot.Bullet;
import cn.tede.javagame.shoot.Enermy;
import cn.tede.javagame.shoot.Heart;
import java.util.List;
import java.util.ArrayList;

import javax.swing.*;
import java.awt.*;

public class Hero {
    //属性
    int x,y;
    int life,magic,score,gold;
    int fire = 2;//1代表单倍火力  2代表双倍火力
    Image heroImg;
    Image[] heroImgs = new Image[10];
    //构造
    public Hero(){
        this.x = 200;
        this.y = 500;
        this.life = 100;
        this.magic = 100;
        this.score = 0;
        this.gold = 0;
        this.heroImg = new ImageIcon("pjdz/Pic/ws00.png").getImage();
        for(int i =0;i<heroImgs.length;i++){
            heroImgs[i] = new  ImageIcon("pjdz/Pic/ws0"+i+".png").getImage();
        }
    }
    //方法
    public void move(int x,int y){
        this.x = x;
        this.y = y;
        if(this.x>=ShootFrame.WIDTH){
            this.x=ShootFrame.WIDTH;
        }else if(this.x<=0){
            this.x=0;
        }
    }
    //实现英雄机的动态效果,此方法需要不停被调用
    //方法内部每一次调用，从一个容器中依次取出一个图片，赋值给heroImag属性。
    int index=0;
    public void step() {
        index++;
        if (index == Integer.MAX_VALUE) {
            index = 0;
        }
        heroImg = heroImgs[index / 15 % heroImgs.length];
    }
    //发射子弹
    public List<Bullet> shoot(){
        //得到英雄机的嘴巴位置坐标，创建一个子弹
        //Bullet bullet = new Bullet();
        //根据活力值发射子弹
        /*
        创建一个集合
        *根据活力值创建一个子弹或多个子弹，添加到集合中
        *最后返回这个集合
         */
        List<Bullet> bullets = new ArrayList<>();
        if(fire==1){
            //找到英雄机的嘴巴位置坐标
            int x=this.x+this.heroImg.getWidth(null)/2;
            int y=this.y;
            Bullet bullet = new Bullet(x,y);
            bullets.add(bullet);
        }
        if(fire==2){
            //找到英雄机的左上角和右上角的坐标
            int x1=this.x;
            int y1=this.y;
            int x2=this.x+this.heroImg.getWidth(null);
            int y2=this.y;
            Bullet bullet1=new Bullet(x1,y1);
            Bullet bullet2=new Bullet(x2,y2);
            bullets.add(bullet1);
            bullets.add(bullet2);
        }

        return bullets;
    }
    //碰到了敌机，生命值减少
    public void hitEnemy(Enermy enermy){
        if (this.x >= enermy.x && this.x <= enermy.x + enermy.enemyImag.getWidth(null) &&
                this.y >= enermy.y && this.y <= enermy.y + enermy.enemyImag.getHeight(null)) {
            this.life -= 1;
        }
    }
    //碰撞了小红心，就增加奖励
    public void hitHeart(Heart heart){
        if(x>=this.x||x<=this.x+40){
            life++;
        }
    }
}
